package com.biz.primus.management.commodity.controller;

import com.biz.primus.base.vo.price.PriceItemVo;
import com.biz.primus.commodity.vo.CommodityVo;
import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.management.base.controller.AbstractBaseController;
import com.biz.primus.management.commodity.feign.CommodityFeignClient;
import com.biz.primus.management.commodity.feign.PriceFeignClient;
import com.biz.primus.model.price.vo.PriceQueryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * 价格管理
 *
 * @author lining
 * @Description: TODO
 * @create 2017/11/22
 */
@Controller
@RequestMapping("management/price")
public class PriceController extends AbstractBaseController {
    private final PriceFeignClient priceFeignClient;

    private final CommodityFeignClient commodityFeignClient;

    @Autowired
    public PriceController(PriceFeignClient priceFeignClient, CommodityFeignClient commodityFeignClient) {
        this.priceFeignClient = priceFeignClient;
        this.commodityFeignClient = commodityFeignClient;
    }

    @GetMapping(value = "list")
    public ModelAndView list(@ModelAttribute("reqVo") PriceQueryVo reqVo) {
        SpringPageVO<PriceItemVo> pricePage = priceFeignClient.findAllPrice(reqVo);
        for (PriceItemVo priceItemVo : pricePage.getContent()) {
            if (StringUtils.isNotBlank(priceItemVo.getProductCode())) {
                List<String> codes = new ArrayList<>();
                codes.add(priceItemVo.getProductCode());
                List<CommodityVo> commodityVos=commodityFeignClient.findByCodesFromDb(codes);
                if(commodityVos!=null&&commodityVos.size()!=0){
                    priceItemVo.setProductName(commodityVos.get(0).getName());
                }
            }
            priceItemVo.setWalletPrice(priceItemVo.getEWalletPrice());
        }
        return new ModelAndView("price/list").addObject("pages", pricePage);
    }
}
